package cc.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;

/**
 * @ClassName: FileImageUtil
 * @Description:
 * @Author: 晓东√
 * @Date: 2024-09-05 09:45
 * @Version: 1.0
 **/
public class FileImageUtil {

    public static String fileUploadUrl(HttpServletRequest req, String path, Part part){
        //指定上传路径
        String fileUploadUrl = req.getSession().getServletContext().getRealPath(path);
        //指定上传路径
        //logger.debug("指定上传路径" + fileUploadUrl);
        System.out.println("指定上传路径" + fileUploadUrl);
        //file流判断文件夹是否存在
        File file = new File(fileUploadUrl);
        //判断上传路径是否存在，不存在新建目录
        if(!file.exists()){
            file.mkdir();
        }
        //获取上传文件名
        String fileName = FileUtil.jxFileName(part);
        //将文件上传服务器
        try {
            part.write(fileUploadUrl+File.separator+fileName);
           return  path+"/"+fileName;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
